package com.pn.controller;

import com.pn.entity.CurrentUser;
import com.pn.entity.Result;
import com.pn.entity.User;
import com.pn.entity.dto.AssignRoleDto;
import com.pn.page.Page;
import com.pn.service.RoleService;
import com.pn.service.UserService;
import com.pn.utils.TokenUtils;
import com.pn.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {

    // 注入UserService
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private TokenUtils tokenUtils;

    // 分页查询用户url接口/user/user-list
    @RequestMapping("/user-list")
    public Result userList(Page page, User user) {

        // 执行业务
        page = userService.queryUserByPage(page, user);
        return Result.ok(page);
    }


    // 添加用户
    @RequestMapping("/addUser")
    @ResponseBody
    public Result addUser(@RequestBody User user, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {

        // 解析token，拿到当前登录用户对象
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int loginUserId = currentUser.getUserId();
        user.setCreateBy(loginUserId);

        // 执行业务
        return userService.saveUser(user);
    }

    // 更新用户状态 0：未启用 1：启用
    @RequestMapping("/updateState")
    @ResponseBody
    public Result updateUserState(@RequestBody User user, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {

        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int loginUserId = currentUser.getUserId();
        user.setUpdateBy(loginUserId);

        // 执行业务方法
        return userService.setUserState(user);
    }

    // 单个删除用户 0未删除 1已删除
    @RequestMapping("/deleteUser/{userId}")
    public Result deleteUserById(@PathVariable Integer userId) {

        return userService.removeUserByIds(Collections.singletonList(userId));
    }

    // 批量删除用户 0未删除 1已删除
    @RequestMapping("/deleteUserList")
    public Result deleteUserByIds(@RequestBody List<Integer> ids) {

        return userService.removeUserByIds(ids);
    }

    @RequestMapping("/updateUser")
    public Result updateUser(@RequestBody User user, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        // 解析token
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        int loginUserId = currentUser.getUserId();
        user.setUpdateBy(loginUserId);
        return userService.setUserByUid(user);
    }

    @RequestMapping("/updatePwd/{userId}")
    public Result updatePwd(@PathVariable Integer userId) {

        return userService.setPwdByUid(userId);
    }

    @RequestMapping("/user-role-list/{userId}")
    public Result userRoleList(@PathVariable Integer userId) {

        return Result.ok(roleService.queryUserRoleByUid(userId));
    }

    @RequestMapping("/assignRole")
    public Result assignRole(@RequestBody AssignRoleDto assignRoleDto) {
        userService.assignRole(assignRoleDto);
        return Result.ok("分配角色成功！");
    }
}
